home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl5
-
- # Copyright 2002, Silicon Graphics, Inc.
- # All Rights Reserved.
- #
- # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- # the contents of this file may not be disclosed to third parties, copied or
- # duplicated in any form, in whole or in part, without the prior written
- # permission of Silicon Graphics, Inc.
- #
- # RESTRICTED RIGHTS LEGEND:
- # Use, duplication or disclosure by the Government is subject to restrictions
- # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- # rights reserved under the Copyright Laws of the United States.
-
- # Updates figures among the print, orig, and online directories for a book
-
- $| = 1;
-
- $g_force_update = 0;
- $g_verbose = 0;
- $g_error_count = 0;
-
- # Could have a function that all conversions pass through that can change
- # the processing based on the expected action.
- $g_action = 'update';
-
- # Changes to this variable will affect existing books and
- # should be made with careful planning.
- $g_gif_comment = 'ag'; # ag is short for autogenerated
-
- $eps2gif = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/eps2gif";
- $gifcomment = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/gifcomment";
- $togif = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/togif";
- $makebw = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/makebw";
-
- while($arg = shift(@ARGV)) {
- if($arg eq '-v') {
- $g_verbose = 1;
- } elsif($arg eq '-u') {
- $g_force_update = 1;
- } elsif($arg eq 'clobber') {
- $g_action = 'clobber';
- } else {
- die "Unknown argument.\n";
- }
- }
-
- @print = ReadDirectory('print');
- @orig = ReadDirectory('orig');
-
- # Make sure output directories are created
- if(@orig || @print) {
- if(! -d 'online') {
- RunCmd("mkdir online");
- die "Couldn't create an 'print' directory.\n" if(! -d 'online');
- }
- if(! -d 'print') {
- RunCmd("mkdir print");
- die "Couldn't create a 'print' directory.\n" if(! -d 'print');
- }
- }
-
- foreach $fig (sort(@orig)) {
- ($base, $suffix) = FilenameInfo($fig);
-
- if($suffix eq 'rgb') {
- rgb2bw($fig, "print/$base.bw");
- rgb2gif($fig, "online/$base.gif");
- }
- }
-
- foreach $fig (sort(@print)) {
- ($base, $suffix) = FilenameInfo($fig);
-
- if($suffix eq 'bw') {
- # Convert to online figure only if not derived from RGB
- if(-e "orig/$base.rgb") { next; }
- rgb2gif($fig, "online/$base.gif");
-
- } elsif($suffix eq 'rgb') {
- rgb2gif($fig, "online/$base.gif");
-
- } elsif($suffix =~ m#^(eps|ps|ai)$#i) {
- eps2gif($fig, "online/$base.gif");
-
- } elsif($suffix =~ m#^(eps|ps|ai)(\.gz)$#i) {
- eps_gz2gif($fig, "online/$base.gif");
- }
- }
-
- exit($g_error_count);
-
- ############################################################################
- #
- # Perform the actual EPS to GIF conversion. The EPS file may be a
- # temporary uncompressed file.
- #
- ############################################################################
- sub real_eps2gif {
- my($eps, $gif) = @_;
-
- print "$eps -> $gif\n";
-
- RunCmd("$eps2gif $eps $gif", $gif);
-
- AddGIFComment($gif);
- }
-
- ############################################################################
- #
- # Verify the GIF file should be updated and call the real conversion.
- #
- ############################################################################
- sub eps2gif {
- my($eps, $gif) = @_;
-
- if($g_action eq 'clobber') {
- ClobberFile($gif);
- return;
- }
-
- return if(! NeedsUpdate($eps, $gif));
-
- return(real_eps2gif($eps, $gif));
- }
-
- ############################################################################
- #
- # Check if the GIF file should be updated. If an update is needed,
- # uncompress the gzipped file to a temporary file and call the
- # real conversion.
- #
- ############################################################################
- sub eps_gz2gif {
- my($eps_gz, $gif) = @_;
-
- if($g_action eq 'clobber') {
- ClobberFile($gif);
- return;
- }
-
- return if(! NeedsUpdate($eps_gz, $gif));
-
- my($tmp_eps) = $eps_gz;
- $tmp_eps =~ s#\.([^\.]+)\.gz$#\.tmp\.$1#;
-
- unlink($tmp_eps);
- RunCmd("gzip -d -c $eps_gz > $tmp_eps", $tmp_eps);
-
- real_eps2gif($tmp_eps, $gif);
-
- unlink($tmp_eps);
- }
-
- ############################################################################
- #
- # Verify the BW file should be updated and run the conversion.
- #
- ############################################################################
- sub rgb2bw {
- my($rgb, $bw) = @_;
-
- if($g_action eq 'clobber') {
- ClobberFile($bw);
- return;
- }
-
- return if(! NeedsUpdate($rgb, $bw));
-
- print "$rgb -> $bw\n";
-
- RunCmd("$makebw $rgb $bw", $bw);
- }
-
- ############################################################################
- #
- # Verify the GIF file should be updated.
- #
- # Note: BW files are just grayscale RGB files
- #
- ############################################################################
- sub rgb2gif {
- my($rgb, $gif) = @_;
-
- if($g_action eq 'clobber') {
- ClobberFile($gif);
- return;
- }
-
- return if(! NeedsUpdate($rgb, $gif));
-
- print "$rgb -> $gif\n";
-
- RunCmd("$togif $rgb $gif -u", $gif);
-
- AddGIFComment($gif);
- }
-
- ############################################################################
- #
- # Add the globally define marker comment to a GIF file. The marker will
- # be read by this program in the future to determine if the file can be
- # overwritten.
- #
- ############################################################################
- sub AddGIFComment {
- my($gif) = @_;
-
- # earlier error messages reported the problem.
- return if(! -w $gif);
-
- RunCmd("$gifcomment -c $g_gif_comment $gif", $gif);
- }
-
- ############################################################################
- #
- # Check if the source ($src) file is newer than the destination ($dest)
- # file. If the destination file doesn't exist or the global $g_force_update
- # variable is true, an update will always be performed.
- #
- # Returns 1 if an update is needed and 0 otherwise.
- #
- ############################################################################
- sub NeedsUpdate {
- my($src, $dest) = @_;
-
- my($too_old) = 0;
- my($writable) = 1;
- my($exists) = 0;
-
- if(-e $dest) {
- $exists = 1;
- $writable = 0 if(! -w $dest);
- }
-
- if($g_force_update) {
- $too_old = 1;
- } elsif(! $exists) {
- $too_old = 1;
- } else {
- my($src_timestamp, $dest_timestamp) = 0;
-
- $src_timestamp = (stat($src))[9];
- $dest_timestamp = (stat($dest))[9];
-
- if($src_timestamp > $dest_timestamp) {
- $too_old = 1;
- }
- }
-
- # If the destintation is more recent, no update is needed
- if(! $too_old) {
- #print "$dest is already up to date.\n" if($g_verbose);
- print "[Current] $dest\n" if($g_verbose);
- return(0);
- }
-
- if($exists && $dest =~ m#\.gif$#) {
- # read the comment
- $comment = `$gifcomment $dest`;
- chomp $comment;
- #print "Comment = [$comment]\n";
- if($comment ne $g_gif_comment) {
- #print "$dest is manually updated\n" if($g_verbose);
- print "[Manual] $dest\n" if($g_verbose);
- system("touch $dest");
- return(0);
- }
- }
-
- if(! $writable) {
- warn "[Error] $dest is not writable - no update is possible.\n";
- ++$g_error_count;
- return(0);
- }
-
- return(1);
- }
-
- ############################################################################
- #
- # Run any command ($cmd) and verify the command ran successfully. All
- # output from the command will be redirected to a temporary file which
- # will be displayed only if an error is detected.
- #
- # Error 1: A non-zero return value from the command
- # Error 2: Optionally the output file name ($expected) can be provided
- # to verify a non-empty file was created.
- #
- # The global $g_error_count variable is incremented for any error.
- #
- ############################################################################
- sub RunCmd {
- my($cmd, $expected) = @_;
- my($error_log, $result);
- my($error_msg) = '';
-
- if($expected) {
- $error_log = "$expected.err";
- } else {
- $error_log = "/var/tmp/$$.fig.err";
- }
- unlink($error_log);
-
- # parenthesis are needed around the command to make both
- # STDOUT and STDERR go into the error log file.
- $result = system("( $cmd 2>&1 ) > $error_log");
-
- if($result >= 256) {
- $result /= 256;
- $error_msg .= "Failure Creating: $expected\n" if($expected);
- $error_msg .= "Running the following command failed with error code '$result'.\n";
- $error_msg .= "\n$cmd\n\n";
- } elsif($result & 0xff) {
- $result &= ~0x80;
- $error_msg .= "The following command was stopped with signal '$result'\n";
- $error_msg .= "See the IRIX signal(5) man page for more information.\n";
- $error_msg .= "\n$cmd\n\n";
- $error_msg .= "All figure processing will be stopped.\n";
- print "\n$error_msg\n";
- unlink($expected) if($expected);
- exit(1);
- } elsif($expected && ! -s $expected) {
- $error_msg .= "The expected file, $expected, is not valid.\n";
- $error_msg .= "The following command was used:\n\n$cmd\n\n";
- }
-
- if($error_msg ne '') {
- ShowErrors($error_log, $error_msg);
- unlink($expected) if($expected);
- ++$g_error_count;
- }
-
- unlink($error_log);
- }
-
- ############################################################################
- #
- # Display the contents of the error report file ($error_file) along with
- # additional information ($msg) that explains the problem.
- #
- # If the error appears to have been caused by a missing Impressario license
- # a message with a better explanation is used instead.
- #
- ############################################################################
- sub ShowErrors {
- my($error_file, $msg) = @_;
-
- if(! open(ERR, $error_file)) {
- $msg .= "Failed to open error report: '$error_file'\n";
- } else {
- $msg .= join('', <ERR>);
- close(ERR);
- }
-
- # Check for the occasional case of Impressario licensing errors
- if($msg =~ m#FLEXlm error# && $msg =~ m#Impressario#) {
- $msg = "The Impressario FLEXlm license is not installed on this system.\n";
- $msg .= "Please install an Impressario license or switch to a machine with one.\n";
- }
-
- print STDERR "\n--------------------------- ERRORS -------------------------\n";
- print STDERR "$msg";
- print STDERR "------------------------------------------------------------\n\n";
- }
-
- ############################################################################
- #
- # Remove a file and verify it has been removed. The file will not be
- # removed unless the user has write access.
- #
- ############################################################################
- sub ClobberFile {
- my($file) = @_;
-
- if(! -e $file) {
- return;
- } elsif(! -w $file) {
- print STDERR "No write access to remove '$file'\n";
- ++$g_error_count;
- return;
- }
-
- if($file =~ m#\.gif$#) {
- # read the comment
- $comment = `$gifcomment $file`;
- chomp $comment;
- if($comment ne $g_gif_comment) {
- return;
- }
- }
-
- print "Removing '$file'\n" if($g_verbose);
- unlink($file);
-
- if(-e $file) {
- print STDERR "Unable to remove '$file'\n";
- ++$g_error_count;
- }
- }
-
- ############################################################################
- #
- # Read a directory and return an array of the contents. If the directory
- # doesn't exist, an empty array will be returned.
- #
- ############################################################################
- sub ReadDirectory {
- my($directory) = @_;
- my(@results) = ();
- my($entry) = '';
-
- return(@results) if(! -d $directory);
-
- opendir(DIR, $directory) || die "Can't open directory '$directory'\n";
- while($entry = readdir(DIR)) {
- if($entry eq '.' || $entry eq '..') { next; }
- push(@results, "$directory/$entry");
- }
- closedir(DIR);
-
- return(@results);
- }
-
- ############################################################################
- #
- # Given a filename ($file) which may include a path portion, return the
- # three portions of the filename:
- # 1. basename of the file
- # 2. suffix (may be empty; includes '.gz' if it exists)
- # 3. directory path (may be empty)
- #
- ############################################################################
- sub FilenameInfo {
- my($file) = @_;
- my($suffix) = '';
- my($path) = '';
-
- # split the path and the filename
- if($file =~ m#^(.*?)/([^/]+)$#) {
- $path = $1;
- $file = $2;
- }
-
- # split the suffix and the base filename
- if($file =~ m#^[^\.]+\.#) {
- # special case to include '.gz' in addition to the
- # real filetype suffix
- $file =~ s#\.([^\.]+(\.gz)?)$##;
- $suffix = $1;
- }
-
- return($file, $suffix, $path);
- }
-